package com.blog.backend.service.impl;

import com.blog.backend.config.CodeMsg;
import com.blog.backend.config.RedisValueConfig;
import com.blog.backend.exception.GlobalException;
import com.blog.backend.mapper.BlogArticleMapper;
import com.blog.backend.mapper.BlogPersonMapper;
import com.blog.backend.mapper.BlogPlateMapper;
import com.blog.backend.mapper.ClassifyTagMapper;
import com.blog.backend.model.BlogArticle;
import com.blog.backend.model.BlogPlate;
import com.blog.backend.req.ArticleListReq;
import com.blog.backend.req.EditArticleReq;
import com.blog.backend.req.InsertArticleReq;
import com.blog.backend.resp.ArticleResp;
import com.blog.backend.resp.TagResp;
import com.blog.backend.service.ArticlesService;
import com.blog.backend.utils.PageUtils;
import com.blog.backend.utils.RedisUtils;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author fanggongsai
 * @description
 * @Date 2021-07-17
 */

@Service
@Slf4j
public class ArticlesServiceImpl implements ArticlesService {

    @Resource
    private BlogArticleMapper blogArticleMapper;

    @Resource
    private BlogPlateMapper blogPlateMapper;

    @Resource
    private BlogPersonMapper blogPersonMapper;

    @Resource
    private ClassifyTagMapper classifyTagMapper;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public void insertArticle(InsertArticleReq req) {
        log.info("新增文章:{}",req.getArticleName());
        if (!StringUtils.isEmpty(req.getPlateId())){
            BlogPlate blogPlate = blogPlateMapper.selectByPrimaryKey(req.getPlateId());
            if (ObjectUtils.isEmpty(blogPlate)){
                throw new GlobalException(CodeMsg.PLATE_INFO_ERROR);
            }
        }
        BlogArticle blogArticle = new BlogArticle();
        blogArticle.setArticleId(UuidUtils.getId());
        blogArticle.setArticleName(req.getArticleName());
        blogArticle.setArticleContent(req.getArticleContent());
        blogArticle.setPlateId(req.getPlateId());
        blogArticle.setLabel(req.getLabel());
        blogArticle.setArticleDescription(req.getArticleDescription());
        blogArticle.setStatus(req.getStatus());
        blogArticleMapper.insertSelective(blogArticle);
    }

    @Override
    public PageResult<ArticleResp> queryArticleList(ArticleListReq req) {
        log.info("文章列表参数:{}",req);
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<ArticleResp> list = blogArticleMapper.selectList(req);
        if (CollectionUtils.isEmpty(list)){
            return new PageResult<>();
        }
//        list.forEach(item->{
//            if (!StringUtils.isEmpty(item.getLabel())){
//                //将标签字符串转成列表
//                List<TagResp> list1 = classifyTagMapper.selectTagList(item.getLabel());
//                item.setLabelList(list1);
//            }
//        });
        PageInfo<ArticleResp> info=new PageInfo<ArticleResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }

    @Override
    public ArticleResp queryDetail(String articleId,Integer status) {
        log.info("文章详情:{}",articleId);
        ArticleResp detail=blogArticleMapper.selectDetail(articleId,status);
        if (ObjectUtils.isEmpty(detail)){
            throw new GlobalException(CodeMsg.ARTICLE_PARAMETER_ERROR);
        }
        if (!StringUtils.isEmpty(detail.getLabel())){
            //将标签字符串转成列表
            List<TagResp> list1 = classifyTagMapper.selectTag(detail.getLabel());
            detail.setLabelList(list1);
        }
        //查询昵称
       String nickName= blogPersonMapper.selectNickName();
        detail.setNickName(nickName);
        return detail;
    }

    @Override
    public void editArticle(EditArticleReq req) {
        log.info("修改文章内容:{}",req.getArticleId());
        BlogArticle blogArticle = blogArticleMapper.selectByPrimaryKey(req.getArticleId());
        if (ObjectUtils.isEmpty(blogArticle)){
            throw new GlobalException(CodeMsg.ARTICLE_PARAMETER_ERROR);
        }
        blogArticle.setLabel(req.getLabel());
        blogArticle.setArticleName(req.getArticleName());
        blogArticle.setArticleContent(req.getArticleContent());
        blogArticle.setPlateId(req.getPlateId());
        blogArticle.setUpdateTime(new Date());
        blogArticle.setImage(req.getImage());
        blogArticle.setArticleDescription(req.getArticleDescription());
        blogArticle.setStatus(req.getStatus());
        blogArticleMapper.updateByPrimaryKeyWithBLOBs(blogArticle);
    }

    @Override
    public void deleteArticle(String articleId) {
        log.info("删除文章:{}",articleId);
        BlogArticle blogArticle = blogArticleMapper.selectByPrimaryKey(articleId);
        if (ObjectUtils.isEmpty(blogArticle)){
            throw new GlobalException(CodeMsg.ARTICLE_PARAMETER_ERROR);
        }
        blogArticleMapper.updateStatus(articleId);
    }

    @Override
    public PageResult<ArticleResp> getTechnologyList(ArticleListReq req) {
        PageResult<ArticleResp> result = this.queryArticleList(req);
        return result;
    }

    @Override
    public ArticleResp getNextArticle(String technologyId) {
        log.info("文章详情:{}",technologyId);
        ArticleResp detail=blogArticleMapper.selectDetail(technologyId,2);
        if (ObjectUtils.isEmpty(detail)){
            throw new GlobalException(CodeMsg.ARTICLE_PARAMETER_ERROR);
        }
        ArticleResp resp= blogArticleMapper.getNextArticle(detail.getCreateTime());
        return resp;
    }

    @Override
    public List<ArticleResp> recommendArticleList() {
        List<ArticleResp>  list=  blogArticleMapper.selectRecommendArticle();
        return list;
    }

}
